﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using App_Code;

public partial class site : System.Web.UI.MasterPage
{
    dulieuDataContext ds = new dulieuDataContext();
    protected int iCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrmenu.Text = LoadMenu();
            drdmenucha.DataSource = from o in ds.Cates where o.Parent == 0 && o.TypeCateID == 2 select o;
            drdmenucha.DataTextField = "Name";
            drdmenucha.DataValueField = "Cate_ID";
            drdmenucha.DataBind();
            //LoadMenuCon();
            rpt_menu_top.DataSource = (from o in ds.Cates orderby o.Cate_ID ascending where o.TypeCateID == 1 select o).Take(5);
            rpt_menu_top.DataBind();
            rpt_khuyen_mai.DataSource = (from o in ds.News where o.Cate_ID == 8 select o).Take(10);
            rpt_khuyen_mai.DataBind();
            lblsosp.Text = (from o in ds.Products select o.ProductID).Count().ToString();
            rpt_tin_moi_nhat.DataSource = (from o in ds.News orderby o.NewsID descending select o).Take(10);
            rpt_tin_moi_nhat.DataBind();
            rpt_thong_tin_cong_ty.DataSource = from o in ds.InfoCompanies where o.id == 1 select o;
            rpt_thong_tin_cong_ty.DataBind();
            rpthuongdan.DataSource = from o in ds.News where o.Cate_ID == 108 select o;
            rpthuongdan.DataBind();
            rptbaohanh.DataSource = from o in ds.News where o.Cate_ID == 109 select o;
            rptbaohanh.DataBind();
            rptthongtinweb.DataSource = from o in ds.News where o.Cate_ID == 110 select o;
            rptthongtinweb.DataBind();
            LoadAnhTraiPhai();
            rpt_san_pham_khuyen_mai.DataSource = (from o in ds.Products
                                                  orderby o.ProductID descending
                                                  where o.Km != ""
                                                  select o).Take(25);
            rpt_san_pham_khuyen_mai.DataBind();

            //add
            Selected_Dropdownlist();
        }

        var cart = (Cart)Session["cart"];
        if(cart!=null)
            iCount = cart.ToDataTable().Rows.Count;
    }
    //add
    private void Selected_Dropdownlist()
    {
        var nhom = Request.QueryString["nhom-san-pham"];
        var hang = Request.QueryString["hang-san-xuat"];
        var gia = (Request.QueryString["gia"]);

        if (drdmenucha.Items.FindByValue(nhom) != null)
        {
            drdmenucha.ClearSelection();
            drdmenucha.Items.FindByValue(nhom).Selected = true;
        }
        LoadMenuCon();
        if (drdmenucon.Items.FindByValue(hang) != null)
        {
            drdmenucon.ClearSelection();
            drdmenucon.Items.FindByValue(hang).Selected = true;
        }

        if (drdlgia.Items.FindByValue(gia) != null)
        {
            drdlgia.ClearSelection();
            drdlgia.Items.FindByValue(gia).Selected = true;
        }
    }

    protected void LoadAnhTraiPhai()
    {
        //rpt_Anh_Trai.DataSource = (from o in ds.Imgs orderby o.IdImage descending where o.ID == 2 select o).Take(1);
        //rpt_Anh_Trai.DataBind();
        //rpt_Anh_Phai.DataSource = (from o in ds.Imgs orderby o.IdImage descending where o.ID == 2 select o).Take(1);
        //rpt_Anh_Phai.DataBind();
        rpt_banner.DataSource = (from o in ds.Imgs orderby o.IdImage descending where o.ID == 1 select o).Take(1);
        rpt_banner.DataBind();

    }
    protected void LoadMenuCon()
    {
        drdmenucon.DataSource = from o in ds.Cates where (o.TypeCateID == 2) && (o.Parent == Convert.ToInt32(drdmenucha.SelectedValue)) select o;
        drdmenucon.DataTextField = "Name";
        drdmenucon.DataValueField = "Cate_ID";
        drdmenucon.DataBind();

    }
    private string LoadMenu()
    {
        var htmlMenu = "";
        var mnupa = from o in ds.Cates where o.Parent == 0 && o.TypeCateID == 2 select o;
        var cmd = (SqlCommand)ds.GetCommand(mnupa);
        var da = new SqlDataAdapter(cmd);
        var ds1 = new DataSet();
        da.Fill(ds1);
        if (ds1.Tables[0].Rows.Count > 0)
        {

            for (var n = 0; n < ds1.Tables[0].Rows.Count; n++)
            {
                var cateId = ds1.Tables[0].Rows[n][0].ToString();
                htmlMenu += "<div class='menutitle' onclick=SwitchMenu('sub" + (n + 1) + "')>" + ds1.Tables[0].Rows[n][2] + "</div>";
                var mnuchild = from o in ds.Cates where o.Parent == Convert.ToInt16(cateId) select o;
                var cmd1 = (SqlCommand)ds.GetCommand(mnuchild);
                var da2 = new SqlDataAdapter(cmd1);
                var ds2 = new DataSet();
                da2.Fill(ds2);
                if (ds2.Tables[0].Rows.Count > 0)
                {
                    htmlMenu += "<span class='submenu' id='sub" + (n + 1) + "'>";
                    htmlMenu += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                    for (var n2 = 0; n2 < ds2.Tables[0].Rows.Count; n2++)
                    {
                        htmlMenu += "<tr><td class='submenu2'><a href='product.aspx?nhom-san-pham=" + ds2.Tables[0].Rows[n2][0] + "' class='linkmn2'>" + ds2.Tables[0].Rows[n2][2] + "</a></td></tr>";
                    }
                    htmlMenu += "</table></span>";
                }
            }
        }
        return htmlMenu;
    }
    protected void drdmenucha_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadMenuCon();
    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        Response.Redirect("search.aspx?nhom-san-pham=" + drdmenucha.SelectedValue + "&&hang-san-xuat=" + drdmenucon.SelectedValue + "&&gia=" + drdlgia.SelectedValue + "");
    }
}
